package com.jd.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import com.jd.entity.WxPayConfig;
import com.jd.service.UserService;
@Service
public class PaymentService {
	@Autowired
	private UserService userService;
	
    public Map<String, String> wxPayAssembly(String userId,String randomNum, String title, String orderNumber, BigDecimal nowPriceBig,HttpServletRequest request) throws Exception {
    	
        WxPayConfig ourWxPayConfig = new WxPayConfig();
        WXPay wxPay = new WXPay(ourWxPayConfig);

        Map<String, String> data = new HashMap<String, String>();
        data.put("appid", ourWxPayConfig.getAppID());
        
        HashMap<String, Object> user = userService.getUser(userId);
        data.put("openid", user.get("open_id").toString());
        data.put("mch_id", ourWxPayConfig.getMchID());
        data.put("trade_type", "JSAPI");
        data.put("notify_url", WxPayConfig.NotifyUrl);
        data.put("spbill_create_ip", "127.0.0.1");
        BigDecimal multiply = nowPriceBig.multiply(new BigDecimal(100));
        System.out.println("模拟订单价格单位分++++++++++++++"+multiply);
        System.out.println("模拟订单价格单位分++++++++++++++"+multiply);
        System.out.println("模拟订单价格单位分++++++++++++++"+multiply);
        String total_fee = new DecimalFormat("#0.##").format(multiply);
        data.put("total_fee", total_fee);
        data.put("out_trade_no", orderNumber);
        data.put("body", title);
        data.put("nonce_str", randomNum);
        String s = WXPayUtil.generateSignature(data, ourWxPayConfig.getKey());
        data.put("sign",s);
        System.out.println(data+"     00000000000000000000000000000000000000000000");
        System.out.println(data+"     00000000000000000000000000000000000000000000");
        System.out.println(data+"     00000000000000000000000000000000000000000000");
        System.out.println(data+"     00000000000000000000000000000000000000000000");
        Map<String, String> respData = null;
        Map<String, String> repData = new HashMap<String, String>();
        try {
            respData = wxPay.unifiedOrder(data);
            System.out.println(respData+"666666666666666666666666666666");
            if (respData.get("result_code").equals("SUCCESS")) {
                repData.put("appId", ourWxPayConfig.getAppID());
                repData.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000));
                repData.put("nonceStr", respData.get("nonce_str"));
                repData.put("package", "prepay_id="+respData.get("prepay_id"));
                repData.put("signType", "MD5");
                String sign = WXPayUtil.generateSignature(repData, ourWxPayConfig.getKey()); //绛惧悕
                repData.put("paySign", sign);
                System.out.println(repData+"111111111111111111111111111111111111");
                System.out.println(repData+"111111111111111111111111111111111111");
                System.out.println(repData+"111111111111111111111111111111111111");
                System.out.println(repData+"111111111111111111111111111111111111");
                System.out.println(repData+"111111111111111111111111111111111111");
                return repData;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
		return repData;
    }
}
